package com.newworld.dmsapp2.util;

import java.util.ArrayList;
import java.util.List;

import org.json.JSONException;
import org.json.JSONObject;

import android.content.Context;
import android.util.Log;
import android.widget.Toast;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.newworld.dmsapp2.R;
import com.newworld.dmsapp2.bean.InitProgressFunction;

public class InitFunctionUtil {

	private Context context;
	List<InitProgressFunction> initProgressFunctions;
	
	public InitFunctionUtil(Context context){
		this.context = context;
		initFunctionData();
	}
	public void initFunctionData(){
		TxtReader txtReader = new TxtReader(context,R.raw.taskstate);
		String txtStr = txtReader.getString();
		Log.e("txt", txtStr);
		Toast.makeText(context, txtStr, Toast.LENGTH_SHORT);
		getFunctionData(txtStr);
		
	}
	
	private void getFunctionData(String txtStr){
		
		/*List<InitProgressFunction> findProgressFuncs = DataSupport.findAll(InitProgressFunction.class);
		if(null != findProgressFuncs){
			for(InitProgressFunction pf : findProgressFuncs){
				DataSupport.delete(InitProgressFunction.class, pf.getId());
			}
		}*/
		
		try {
			JSONObject jsonObj = new JSONObject(txtStr);
			initProgressFunctions = new ArrayList<InitProgressFunction>();
			java.lang.reflect.Type type = new TypeToken<List<InitProgressFunction>>() {
			}.getType();
			Gson gson = new Gson();
			initProgressFunctions = gson.fromJson(jsonObj.getString("progressFunctionList"),(java.lang.reflect.Type) type);
			/*DataSupport.saveAll(initProgressFunctions);
			List<InitProgressFunction> findProgressFunc = DataSupport.findAll(InitProgressFunction.class);
			Log.e("saveFunctionData", findProgressFunc.size()+"");*/
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	public List<InitProgressFunction> getProgressFunction(){
		return initProgressFunctions;
	}
}
